package com.example.conf;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.springframework.http.client.SimpleClientHttpRequestFactory;


@Configuration
public class RestTemplateConf extends RestTemplate{

    /**
     * 当指定类型Bean不存在时，会创建一个新的Bean，如果用户自定义了Bean,将使用用户自定义的Bean
     * @return
     */
    @ConditionalOnMissingBean(RestTemplate.class)
    @Bean
    public RestTemplate restTemplate() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setReadTimeout(90 * 1000);
        factory.setConnectTimeout(90 * 1000);
        return new RestTemplate(factory);
    }
}
